//=============================================================================
//     _   _                               ____                _  _          
//    | | | |  __ _  _ __   _ __   _   _  / ___|  _ __   _ __ (_)| |_   ___ 
//    | |_| | / _` || '_ \ | '_ \ | | | | \___ \ | '_ \ | '__|| || __| / _ \
//    |  _  || (_| || |_) || |_) || |_| |  ___) || |_) || |   | || |_ |  __/
//    |_| |_| \__,_|| .__/ | .__/  \__, | |____/ | .__/ |_|   |_| \__| \___|
//                  |_|    |_|     |___/         |_|                         
//
//                     HappySprite - We make sprites happy
//
// Copyright (c) 2007 by Tank Monkey Games
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//=============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace HappySprite.SceneGraph
{
    class LocalTransform : Transform3D
    {
        SceneItem owner;

        internal LocalTransform(SceneItem owner)
            : base()
        {
            this.owner = owner;
        }

        protected override void OnTransformChanged()
        {
            owner.InvalidateWorld();
        }

        internal void UpdateFromWorld()
        {
            if (owner.Parent == null || owner.Parent.World.IsKnownToBeIdentity)
            {
                // Parent world is identity or no parent
                CopyFromCore(owner.World);
            }
            else
            {
                Transform3D parent = owner.Parent.World;
                Transform3D world = owner.World;

                // Update position
                Quaternion inverse = Quaternion.Inverse(parent.Rotation.Quaternion);
                Vector3 localPosition = Vector3.Transform(world.Position, inverse);
                localPosition = Vector3.Divide(localPosition, parent.Scale);

                SetPosition(localPosition);

                // Update rotation
                if (owner.InheritRotation)
                {
                    SetRotation(world.Rotation.Quaternion / parent.Rotation.Quaternion);
                }
                else
                {
                    SetRotation(world.Rotation.Quaternion);
                }

                // Update scale
                if (owner.InheritScale)
                {
                    SetScale(world.Scale / parent.Scale);
                }
                else
                {
                    SetScale(world.Scale);
                }
            }
        }
    }
}
